package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.dto.message.MessageDto;
import com.zecan.blog.service.MessageService;
import com.zecan.blog.utils.BlogResultUtils;
import com.zecan.blog.vo.message.MessageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MessageController
 * @Description
 * @date 2022/11/22 22:10
 * @Version 1.0
 */
@RestController
@RequestMapping("/message")
public class MessageController {


    @Resource
    private MessageService messageService;

    /**
     * 添加留言
     * @param messageDto
     * @return
     */
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public BlogResult addMessage(MessageDto messageDto){
        messageService.addMessage(messageDto);
        return BlogResultUtils.ok("添加留言成功");
    }

    /**
     * 获取留言信息
     * @return
     */
    @RequestMapping(value = "/getMsgList" ,method = RequestMethod.GET)
    public BlogResult getMsgList( ){
        List<MessageVo> messageList = messageService.getMessageList();
        return BlogResultUtils.ok(messageList,"获取留言成功");
    }

    /**
     * 获取留言分页信息
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @RequestMapping(value = "/list_limit" ,method = RequestMethod.GET)
    public BlogResult getMsgList(Integer offset , Integer limit){
        List<MessageVo> messageList = messageService.getMessageByLimit(offset, limit);
        return BlogResultUtils.ok(messageList,"获取留言成功");
    }


    /**
     * 获取留言分页信息
     * @return
     */
    @PreAuthorize("hasAnyAuthority('admin')")
    @RequestMapping(value = "/del" ,method = RequestMethod.GET)
    public BlogResult deleteMessage(Integer messageId){
        messageService.delMessage(messageId);
        return BlogResultUtils.ok("删除留言成功");
    }
}
